# coding=utf-8
import urllib.request
import json
import arrow

# 生成2020一年的节假日，休息日，法定节假日表
def get_holiday_fun():
    ua_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 '
                      'Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,'
                  'application/signed-exchange;v=b3;q=0.9 '
    }

    # holiday接口
    url = "http://timor.tech/api/holiday/year/2020/"
    request = urllib.request.Request(url, headers=ua_headers)
    response = urllib.request.urlopen(request)
    json_str = response.read().decode('utf8')
    print(json_str)

    # 把json转成dict
    json_dict = json.loads(json_str)
    # 获取holiday的dict
    json_holiday_dict = json_dict['holiday']

    holiday_dict = {}
    work_dict = {}
    # 遍历json_holiday_dict，打印每个节日日期
    for k, value in json_holiday_dict.items():
        is_holiday = value['holiday']
        if is_holiday is True:
            date = value['date']
            name = value['name']
            #print(date + "|" + name)
            holiday_dict[date] = name
        else:
            date = value['date']
            name = value['name']
            work_dict[date] = name
    return holiday_dict,work_dict

def isLeapYear(years):
    """
    通过判断闰年，获取年份years下一年的总天数
    :param years: 年份，int
    :return:days_sum，一年的总天数
    """
    # 断言：年份不为整数时，抛出异常。
    assert isinstance(years, int), "请输入整数年，如 2018"

    if (years % 4 == 0 and years % 100 != 0) or (years % 400 == 0):  # 判断是否是闰年
        # print(years, "是闰年")
        days_sum = 366
        return days_sum
    else:
        # print(years, '不是闰年')
        days_sum = 365
        return days_sum


def getAllDayPerYear(years):
    """
    获取一年的所有日期
    :param years:年份
    :return:全部日期列表
    """
    start_date = '%s-1-1' % years
    a = 0
    all_date_list = []
    days_sum = isLeapYear(int(years))
    while a < days_sum:
        b = arrow.get(start_date).shift(days=a).format("YYYY-MM-DD")
        a += 1
        all_date_list.append(b)
    # print(all_date_list)
    return all_date_list


if __name__ == '__main__':
    # years = "2001"
    # years = int(years)
    # # 通过判断闰年，获取一年的总天数
    # days_sum = isLeapYear(years)

    # 获取一年的所有日期
    all_date_list = getAllDayPerYear("2020")
    print(all_date_list)
    holiday_dict , work_dict = get_holiday_fun()
    print(holiday_dict)
    print(work_dict)
    for date in all_date_list:
        if date in holiday_dict:
            print(date + "|" + "True" + "|" + holiday_dict[date])
        elif date in work_dict:
            print(date + "|" + "False" + "|" + work_dict[date])
        else:
            print(date + "|" + "False" + "|" )